package org.dong.control;

import org.dong.Tool.ApplicationUtil;
import org.dong.Tool.Data;
import org.dong.pojo.AdminUser;
import org.dong.pojo.Dto;
import org.dong.pojo.User;
import org.dong.service.AdminUserService;
import org.dong.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AdminUserControl {

    @Resource
    AdminUserService adminUserService;

    @PostMapping("/Admin/Login")
    public Dto AdminLogin(HttpServletRequest req){
        Dto dto = new Dto();


        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String code = req.getParameter("code");


        if(code.equalsIgnoreCase((String) req.getSession().getAttribute("codeStr"))){
            AdminUser adminUser = adminUserService.getAdminUser(account, Data.getMD5Hash(password));
            req.getSession().setAttribute("user",adminUser);
            if ( adminUser != null) {

                Map<String, Object> datas = new HashMap<String, Object>();
                datas.put("user", adminUser);
                dto.setDatas(datas);
                dto.setLocation("index");
                dto.setErrMsg("登录成功");
                dto.setStatusCode(200);

            } else {
                dto.setErrMsg("登录失败");
                dto.setStatusCode(2001);
            }
        }else{
            dto.setErrMsg("验证码错误");
            dto.setStatusCode(2001);
        }

       return dto;
    }

    @PostMapping("/Admin/List")
    public Dto AdminList(HttpServletRequest req){
        Dto dto = new Dto();

        List<AdminUser> adminUserList = adminUserService.getAdminUserList(1);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list",adminUserList);

        dto.setDatas(data);
        dto.setStatusCode(200);
        dto.setErrMsg("管理员列表");

        return dto;
    }

    @PostMapping("/Admin/add")
    public Dto AdminAdd(HttpServletRequest req){
        Dto dto = new Dto();

        String account = req.getParameter("account");
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String pass = req.getParameter("pass");

        //先查询是否有存在的管理员账户
        if(adminUserService.getAdminUser(account,null) == null){
            AdminUser adminUser = new AdminUser();
            adminUser.setAdminName(username);
            adminUser.setAccount(account);
            adminUser.setPhone(phone);
            adminUser.setPassword(Data.getMD5Hash(pass));



            if(adminUserService.addAdminUser(adminUser) > 0){

                dto.setStatusCode(200);
                dto.setErrMsg("新增管理员成功");

            }else{

                dto.setStatusCode(201);
                dto.setErrMsg("新增管理员失败");
            }
        }else{
            dto.setStatusCode(201);
            dto.setErrMsg("账号已经存在");
        }


        return dto;
    }


    @GetMapping("/Admin/getInfo")
    public Dto AdminGetInfo(HttpServletRequest req){
        Dto dto = new Dto();

        String id = req.getParameter("id");

        AdminUser adminUserId = adminUserService.getAdminUserId(Integer.valueOf(id));

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("info",adminUserId);

        dto.setDatas(data);
        dto.setStatusCode(200);
        dto.setErrMsg("用户详情");

        return dto;
    }


}
